<?php

namespace App\Admin\Rules;

use BadMethodCallException;
use App\Admin\Filters\Filter;
use Illuminate\Contracts\Validation\Rule;

class FilterRules implements Rule
{
    protected $errorMessage;

    /**
     * Determine if the filter rule passes.
     *
     * @param string $attribute
     * @param mixed $value
     *
     * @return bool
     */
    public function passes($attribute, $value)
    {
        if (!$value || !is_string($value)) {
            return true;
        }
        $valueArr = json_decode($value, true);
        if (!isset($valueArr[0]) || !in_array($valueArr[0], array_column(Filter::$filterRuleMap,0))) {
            $this->errorMessage = '选项 无效。';
            return false;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return $this->errorMessage;
    }
}
